package top.hzy520.payment.alipay.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import top.hzy520.payment.alipay.dto.RefundInputDto;
import top.hzy520.payment.alipay.service.AliPayService;
import top.hzy520.payment.config.AlipayConfig;
import top.hzy520.payment.alipay.dto.Order;
import top.hzy520.payment.alipay.feign.OrderService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
*@Author: HouZeYu
*@Description: 统一支付业务
*@Date: 16:26 2018/6/1
*/
@Slf4j
@Controller
@RequestMapping("alipay")
public class AlipayController {

    @Autowired
    @Qualifier("aliPayServiceImpl")
    private AliPayService aliPayService;
    /**
    *@Author: HouZeYu
    *@Description: 支付宝退款接口
    *@Date: 10:55 2018/9/16
    */
    @RequestMapping("/repay")
    public String reFund(@RequestBody RefundInputDto payInputDto) throws AlipayApiException{
      return aliPayService.reFund(payInputDto);
    }

    /**
    *@Author: HouZeYu
    *@Description: 支付宝pc支付接口
    *@Date: 13:35 2018/5/22
    */
    @PostMapping("/pay")
    public void pay(HttpServletResponse response, @RequestBody Order order){
         aliPayService.alipayPcPay(order,response);
    }
    /**
    *@Author: HouZeYu
    *@Description:  支付宝wap支付接口
    *@Date: 13:34 2018/5/22
    */
    @RequestMapping("/wappay")
    public void paywap(HttpServletRequest request,HttpServletResponse response) throws IOException {
            // 商户订单号，商户网站订单系统中唯一订单号，必填
            String out_trade_no = "133199";//request.getParameter("out_trade_no");
            // 订单名称，必填

            String subject ="肉夹馍";//request.getParameter("subject");
            System.out.println(subject);
            // 付款金额，必填
            String total_amount="12.33";//request.getParameter("total_amount");
            // 商品描述，可空
            String body ="aa";// request.getParameter("body");
            // 销售产品码 必填
            String product_code="QUICK_WAP_WAY";
            /**********************/
            // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
            //调用RSA签名方式
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
            AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest();

            // 封装请求支付信息
            AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
            model.setOutTradeNo(out_trade_no);
            model.setSubject(subject);
            model.setTotalAmount(total_amount);
            model.setBody(body);
            model.setProductCode(product_code);
            alipay_request.setBizModel(model);
            // 设置异步通知地址
            alipay_request.setNotifyUrl(AlipayConfig.notify_url);
            // 设置同步地址
            alipay_request.setReturnUrl(AlipayConfig.return_url);

            // form表单生产
            String form = "";
            try {
                // 调用SDK生成表单
                form = alipayClient.pageExecute(alipay_request).getBody();
                response.setContentType("text/html;charset=" + AlipayConfig.charset);
                response.getWriter().write(form);//直接将完整的表单html输出到页面
                response.getWriter().flush();
                response.getWriter().close();
            } catch (AlipayApiException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
        /**
        *@Author: HouZeYu
        *@Description: 支付宝异步通知
        *@Date: 13:34 2018/5/22
        */
    @RequestMapping("/notifyUrl")
    public void notifyUrl(HttpServletRequest request, HttpServletResponse response) throws IOException {
        aliPayService.notifyUrl(request,response);
    }
}
